import logo from "@/assets/images/logo.png";
import type { AppSettings, VueMetaEnv } from "@/types/index";
import type { RouteRecordRaw } from "vue-router";

/** 默认系统配置. */
const defaultSettings: AppSettings = reactive({
  title: "jmash-vue3",
  version: "v0.0.1",
  logo: logo,
  showSettings: true,
  tagsView: true,
  fixedHeader: true,
  sidebarLogo: true,
  layout: "left",
  theme: "light",
  size: "default",
  language: "zh-cn",
  themeColor: "#409EFF",
  watermarkEnabled: false,
  watermarkContent: "jmash-vue3"
});

/** 默认Vue MetaEnv 配置. */
const vueMetaEnv: VueMetaEnv = reactive({
  APP_PORT: "",
  APP_BASE_URL: "",
  APP_API_URL: "/",
  APP_BASE_API: "/",
  APP_CLIENT_ID: "pc_vue3",
  //微信公众号APP_ID
  APP_WECHAT_APPID: "",
  //微信三方授权ID
  APP_WECHAT_COMPONENT_ID: "",
  //支付宝APP_ID
  APP_ALIPAY_APPID: "",
  //云闪付APP_ID
  APP_UNIONPAY_APPID: ""
});

//动态路由模块
const dynamicRoutes: RouteRecordRaw[] = [];

/** 添加动态路由. */
export function addDynamicRoute(routes: RouteRecordRaw[]) {
  dynamicRoutes.push(...routes);
}

/** 更新环境配置. */
export function updateMetaEnv(metaEnv: VueMetaEnv) {
  return Object.assign(vueMetaEnv, metaEnv);
}

/** 更新Vite环境配置. */
export function updateViteMetaEnv(viteMetaEnv: ImportMetaEnv): VueMetaEnv {
  const metaEnv: VueMetaEnv = convertViteMetaEnv(viteMetaEnv);
  return updateMetaEnv(metaEnv);
}

/** 转换Vite环境配置. */
export function convertViteMetaEnv(viteMetaEnv: ImportMetaEnv): VueMetaEnv {
  if (viteMetaEnv == undefined) {
    return {} as VueMetaEnv;
  }
  const metaEnv: VueMetaEnv = Object.keys(viteMetaEnv)
    .filter((key) => key.startsWith("VITE_APP_"))
    .reduce((acc: VueMetaEnv, key) => {
      const shortKey = key.replace(/^VITE_/, "");
      return Object.assign(acc, { [shortKey]: viteMetaEnv[key] });
    }, {} as VueMetaEnv);

  return metaEnv;
}

/** 更新系统配置信息. */
export function updateSettings(setting: AppSettings) {
  return Object.assign(defaultSettings, setting);
}

export { vueMetaEnv, defaultSettings, dynamicRoutes };

export default defaultSettings;
